package com.basic.library.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;

import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.basic.library.Interface.MCallback;
import com.google.gson.Gson;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;


/**
 * @author yj
 * @description
 */
public class PermissionUtil {

    /**
     * 判断是否有此权限
     *
     * @param context
     * @param mCallback
     * @param permissions
     */
    public static boolean granted(Context context, MCallback<Boolean> mCallback, String... permissions) {
        if (permissions == null) {
            throw new NullPointerException("记得给权限啊！！！");
        }
        boolean granted = true;
        for (String permission : permissions) {
            granted = granted && ActivityCompat
                    .checkSelfPermission(context, permission)
                    == PackageManager.PERMISSION_GRANTED;
        }
        if (granted && mCallback != null)
            mCallback.onResponse(true);
        return granted;
    }

    /**
     * 判断是否有此权限 否则申请一次
     *
     * @param context
     * @param mCallback
     * @param permissions
     */
    public static void authorOnceWithGranted(Object context, MCallback<Boolean> mCallback, String... permissions) {
        if (permissions == null) {
            throw new NullPointerException("记得给权限啊！！！");
        }
        boolean granted = true;
        Context context1 = null;
        if (context instanceof FragmentActivity)
            context1 = (FragmentActivity) context;
        else if (context instanceof Fragment)
            context1 = ((Fragment) context).getContext();
        for (String permission : permissions) {
            granted = granted && ActivityCompat
                    .checkSelfPermission(context1, permission)
                    == PackageManager.PERMISSION_GRANTED;
        }
        MyLog.p("已经授权" + granted);
        if (mCallback != null) {
            if (granted) {
                mCallback.onResponse(true); //权限都开启了，直接返回
            } else {
                grand_permission(context, mCallback, 1, permissions); //申请权限
            }
        }
    }

    /**
     * 判断是否有此权限 否则持续申请
     *
     * @param context
     * @param mCallback
     * @param permissions
     */
    public static void authorWithGranted(Object context, MCallback<Boolean> mCallback, String... permissions) {
        if (permissions == null) {
            throw new NullPointerException("记得给权限啊！！！");
        }
        boolean granted = true;
        Context context1 = null;
        if (context instanceof FragmentActivity)
            context1 = (FragmentActivity) context;
        else if (context instanceof Fragment)
            context1 = ((Fragment) context).getContext();
        for (String permission : permissions) {
            granted = granted && ActivityCompat
                    .checkSelfPermission(context1, permission)
                    == PackageManager.PERMISSION_GRANTED;
        }
        MyLog.p(permissions + "已经授权" + granted);
        if (mCallback != null) {
            if (granted) {
                mCallback.onResponse(true); //权限都开启了，直接返回
            } else {
                grand_permission(context, mCallback, 3, permissions); //申请权限
            }
        }
    }

    /**
     * 注意同一个页面内最好不要同时多次new RxPermissions(activity)，每个里的权限不要有重复，减少bug
     * 最好是在启动页一次性搞定所有权限的申请，只写一个new RxPermissions(activity)
     *
     * @param activity
     * @param mCallback
     * @param times
     * @param permissions
     */
    @SuppressLint("CheckResult")
    private static void grand_permission(final Object activity, final MCallback<Boolean> mCallback, final int times, final String... permissions) {
        MyLog.p("开始授权--" + new Gson().toJson(permissions));
        RxPermissions rxPermissions = null;
        if (activity instanceof FragmentActivity)
            rxPermissions = new RxPermissions((FragmentActivity) activity);
        else if (activity instanceof Fragment)
            rxPermissions = new RxPermissions((Fragment) activity);
        if (rxPermissions != null)
            rxPermissions.requestEachCombined(permissions).subscribe(new Consumer<Permission>() {
                @Override
                public void accept(Permission aBoolean) throws Exception {
                    MyLog.p(aBoolean.name + "--" + aBoolean.granted);
                    int count = times;
                    if (mCallback != null) {
                        if (aBoolean.granted) {
                            mCallback.onResponse(true); //权限都开启了，直接返回
                        } else {
                            if (count == 1)
                                mCallback.onResponse(false); //一次不成功，直接返回false
                            else if (count > 1) {
                                count = count - 1;
                                grand_permission(activity, mCallback, count, permissions); //申请权限
                            }
                        }
                    }
                }
            });
    }
}

